<script lang="ts" setup>
import { computed } from 'vue';
import { formatNumber } from '@/home/utils/format';
import { useFetch } from '@/home/hooks/fetch';
import { useAsyncData } from '@/home/hooks/async-data';
import TabNavigation from '@/home/components/TabNavigation.vue';
import Pagination from '@/home/components/Pagination.vue';
import { useTitleForClient } from '@/home/hooks/browser';
import LoadingBox from '@/home/components/LoadingBox.vue';
import Loading from '@/home/components/Loading.vue';
import { useI18n } from 'vue-i18n';
import Platform from '@/common/components/platform-avatar/index.vue';

const { t } = useI18n();
const { $fetch } = useFetch();
const { setTitle } = useTitleForClient();

const pathPrefix = '/explore/organization/';
const defaultTitle = t('home.explore.organization.title');
const props = defineProps({
  type: {
    type: String,
    default: '',
  },

  page: {
    type: String,
    default: '',
  },
});

const PAGE_SIZE = 10;
const types = [
  {
    name: 'all',
    value: '-1',
    text: t('home.common.all'),
  },
  {
    name: 'prime',
    value: '1',
    text: t('home.common.prime'),
  },
];

const getCurrentType = () => types.find((item) => item.name === props.type) ?? types[0];
const getTitle = () => `${getCurrentType().text} - ${defaultTitle}`;

const currentType = computed(() => getCurrentType().name);

const { data: listing, pending } = useAsyncData(
  'explore:organization',
  () => $fetch('/org/recommend_list', {
    method: 'post',
    data: {
      pageNum: Number(props.page) || 1,
      pageSize: PAGE_SIZE,
      isSelected: getCurrentType().value,
    },
  }),
  {
    cache: false,
    transform: (data) => data ?? {},
    watch: [() => props.type, () => props.page],
    onAfterHandle({ handleSuccess, serverContext }) {
      if (!handleSuccess) {
        return;
      }

      if (serverContext) {
        serverContext.title = getTitle();
      }

      if (!import.meta.env.SSR) {
        setTitle(getTitle());
      }
    },
  },
);
const getRouterPath = (pageNum: number) => `${pathPrefix}${currentType.value}/${pageNum}`;
</script>

<template>
  <section class="page-explore-organization mt-40 container">
    <TabNavigation :path-prefix="pathPrefix" :items="types" v-model="currentType" />

    <ul class="mt-20 min-h-200 rounded-4 bg-hex-ffffff p-20 shadow-[0_1px_6px_0_rgba(0_0_0_0.04)]">
      <LoadingBox :loading="pending">
        <template v-if="listing?.records?.length > 0">
          <li
            v-for="item in listing.records"
            :key="item.id"
            class="organization mb-20 h-102 border-b-1 border-hex-f7f7f7 pb-20 last-of-type:(mb-0 h-82px border-b-0 pb-0)"
          >
            <div class="flex-y-center">
              <div v-if="item.isSelected" class="mr-8 h-22 flex-shrink-0 rounded-2 bg-hex-E8F4FF px-6 text-center text-12 color-hex-345DE2 font-400 leading-22">
                {{ $t('home.common.prime') }}
              </div>
              <a :href="item.jump" target="_blank">
                <h3 class="organization-title line-clamp-1 break-anywhere text-16 color-hex-292929 font-500 leading-22">
                  {{ item.name }}
                </h3>
              </a>
              <Platform class="ml-4 shrink-0" :platform="item.platform" icon-class="size-16" tooltip />
            </div>

            <p class="line-clamp-1 mt-6 break-anywhere text-14 color-hex-575757 font-400 leading-22">
              {{ item.description ? item.description : $t('home.explore.organization.empty') }}
            </p>

            <div class="mt-10 flex justify-between">
              <div class="flex">
                <div class="mr-20 flex-y-center text-12 color-hex-8b8b8b font-400">
                  <div class="i-icons:repository mr-4 size-16"></div>
                  <div class="h-16">{{ formatNumber(item.warehouseCount) }}</div>
                </div>
                <div class="mr-20 flex-y-center text-12 color-hex-8b8b8b font-400">
                  <div class="i-icons:member mr-4 size-16"></div>
                  <div class="h-16">{{ formatNumber(item.memberCount) }}</div>
                </div>
                <div v-if="item.jump" class="flex-y-center text-12 color-hex-8b8b8b font-400">
                  <div class="i-icons:link mr-4 size-16"></div>
                  <div class="h-16">{{ item.jump }}</div>
                </div>
              </div>
            </div>
          </li>
        </template>

        <template v-else-if="listing?.records?.length === 0">
          <li class="min-h-100 text-center color-hex-8b8b8b">{{ $t('home.common.emptyContent') }}</li>
        </template>

        <template #loading>
          <li class="min-h-100 flex-center">
            <Loading class="size-16 color-hex-8b8b8b" />
          </li>
        </template>
      </LoadingBox>
    </ul>

    <Pagination class="data-[total-pages=gt1]:(mt-20)" :count="listing?.total" :page="Number(props.page) || 1" :page-size="PAGE_SIZE" :get-router-path="getRouterPath" />
  </section>
</template>

<style lang="scss" scoped>
.page-explore-organization {
  .organization {
    &:hover .organization-title {
      color: #345de2;
    }
  }
}
</style>
